<!-- Add Modal //-->
<div id="addModal" class="reveal-modal medium" data-reveal>
	<span id="loadingAdd" style="display:none;">Sedang memproses..</span>
	<span id="errorAdd" style="display:none;color:red;">Ma'af, tidak dapat memuat</span>
	<div id="add_tholabah_modal">		
	</div>
</div>
<!-- End Add Modal //-->

<!-- Delete Modal //-->
<div id="delModal" class="reveal-modal tiny" data-reveal>
	<p class="p-modal">
	<span id="hpid" class="hpid var-modal" style="display:none;"></span>
	<input type="hidden" id="id_a_bulan1" value="<?php echo $id_a_kelas = $this->security->xss_clean($this->uri->segment(3));?>"/>
	<input type="hidden" id="id_m_j_pendidikan1" value="<?php echo $id_a_kelas = $this->security->xss_clean($this->uri->segment(4));?>">
	<input type="hidden" id="id_m_th_ajaran1" value="<?php echo $id_a_kelas = $this->security->xss_clean($this->uri->segment(5));?>">
	Yakin ingin menghapus <span class="nama var-modal"></span> kelas <span class="2nd var-modal"></span> dari penerima <span class="3rd var-modal"></span> 
	untuk bulan <span class="4th var-modal"></span> sebesar <span class="5th var-modal"></span>?
	<br/><br/>
	<span id="loadingDel" style="display:none;">Sedang memproses..</span>
	<span id="errorDel" style="display:none;color:red;">Ma'af, gagal dihapus</span>
	<span class="pull-right">
		<button class="btn btn-xs tmb-utama" id="hapusBagiSubSy"> Yakin</button> 
		<button class="btn tmb-kedua close-reveal-modal close-modal"> Tidak</button></span>
</div>
<!-- End Delete Modal //-->

<div class="row">
	<div class="col-lg-12">
		<div class="panel panel-default kotakwidget">
			<div class="panel-heading kepalakotakwidget">
				Subsidi syahriyah Maddin <?php foreach ($list_tag as $row_tg) {$id_m_th_ajaran = $row_tg->id_m_th_ajaran; $id_m_j_pendidikan = $row_tg->id_m_j_pendidikan; echo $row_tg->j_pendidikan.' bulan '.$row_tg->bulan.' '.$row_tg->th_ajaran;?>
				<span id="jp" style="display:none;"><?php echo $row_tg->j_pendidikan; ?></span>
				<input type="hidden" id="id_a_bulan" value="<?php echo $row_tg->id_a_bulan; ?>"/>
				<input type="hidden" id="id_m_th_ajaran" value="<?php echo $row_tg->id_m_th_ajaran; ?>"/>
				<?php $bulan  = $row_tg->bulan.' '.$row_tg->th_ajaran; ?>
				<div>
					<span class="wali_kelas">
						Besar tagihan <?php echo formatRupiah($row_tg->besar_sy);}?>. Sebanyak <span id="jml"><?php echo count($list);?></span> tholabah telah diberi subsidi
					</span>
					<span class="pull-right tmb_panel_head">
						<?php if($id_m_th_ajaran == id_ta_aktif()){?>	
						<button id="add" onClick="add_tholabah('<?php echo $id_m_j_pendidikan;?>')" data-reveal-id="addModal" data-reveal class="btn btn-sm tmb-tambah tmb-utama"><i class="fa fa-plus"></i> Tambah</button>
						<?php } ?>
					</span>
				</div>				
			</div>
				
				<div class="panel-body">
					<div class="row">
						<div class="col-md-12">
							<div id="contentBagiSubSy">
							<table class="table table-condensed">
							  <thead>
								<tr>
									<th width="30">#</th>
									<th width="80">NIT</th>
									<th>Nama Lengkap</th>
									<th>Jenis Kelamin</th>
									<th>Kelas</th>
									<th>Subsidi</th>
									<th width="123">Besar Subsidi</th>
									<th width="123">Sisa Tagihan</th>
									<th></th>
								</tr>
							  </thead>
							  <tbody>
								<?php 
									if(count($list)>0){
										$i = 1; 
										foreach ($list as $row) {
								?>
								<tr class="hover">
									<td><?php echo $i++ ?></td>
									<td><?php echo $row->id_m_tholabah; ?></td> 
									<td><?php echo $row->nama_lengkap; ?></td> 
									<td><?php echo $row->jk; ?></td>
									<td><?php echo $row->kelas; ?></td>
									<td><?php echo $row->nama_subsidi; ?></td>
									<td><?php echo formatRupiahTabel($row->besar_subsidi); ?></td>
									<td><?php echo formatRupiahTabel($row->sisa_tagihan); ?></td>
									<td>
										<?php if($id_m_th_ajaran == id_ta_aktif()){?>
										<a class="editorLink pull-right" data-reveal-id="delModal" nama="<?php echo $row->nama_lengkap; ?>" 2nd="<?php echo $row->kelas; ?>" 3rd="<?php echo $row->nama_subsidi; ?>" 4th="<?php echo $bulan;?>" 5th="<?php echo formatRupiah($row->besar_subsidi); ?>" hpid="<?php echo $row->id_b_sub_sy;?>" data-reveal><span class="glyphicon glyphicon-floppy-remove"></span></a>
										<?php } ?>
									</td>
								</tr>
								<?php
										}
									}else{
								?>
								<tr>
									<td colspan="9"><center>Belum ada tholabah Maddin <?php foreach ($list_tag as $row_tg) {echo $row_tg->j_pendidikan;?> yang tersubsidi pada bulan <?php echo $row_tg->bulan.' '.$row_tg->th_ajaran;}?></center></td>
								</tr>
								<?php
									}
								?>
							  </tbody>
							</table>
							</div>
						</div>
					</div>
				</div>
		</div>
	</div>	
</div>
<!-- /#page-wrapper -->
<script>
// 
$(document).on('opened', '[data-reveal]', function () {
	//$('body').css({overflow: 'hidden'}); 
});
$(document).on('closed', '[data-reveal]', function () {
	//$('body').css({overflow: ''}); 
	//$('body#addModal').css({overflow: 'scroll !important'}); 
});	

// Menampilkan isi modal tambah
function add_tholabah(id_m_j_pendidikan){
	$.ajax({
	type: "POST",
		data: "id_m_j_pendidikan="+id_m_j_pendidikan,
		url : "<?php echo site_url(); ?>b_sub_sy/subsidi",				
		cache: false,
		beforeSend: function(){
			$('#loadingAdd').show();
			$('#errorAdd').hide();
		}, 
		success: function(response){
			$('#add_tholabah_modal').html(response);
			$('#loadingAdd').hide();	

			var jenjang = $("#jp").text();
			$('#j_pend').text(jenjang);				
		}, 
		error:function(){
			$('#errorAdd').show();
			$('#loadingAdd').hide();
		}
	});
	return false;
}
// Ajax Complete
$(document).ajaxComplete(function(){
	// Format Rp
	$('#besar_subsidi,#sub,#sisa_sub').priceFormat({
	    prefix: 'Rp ',
	    centsLimit: '',
	    thousandsSeparator: '.'
	});
	// Combo pilih Subsidi
	$("#id_a_subsidi").change(function(){
		var id_a_subsidi = $("#id_a_subsidi").val();
		var id_m_j_pendidikan = $("#id_m_j_pendidikan").val();
		$.ajax({
			type: "POST",				
			data: "id_a_subsidi="+id_a_subsidi+"&id_m_j_pendidikan="+id_m_j_pendidikan,
			url : "<?php echo site_url(); ?>b_sub_sy/sisa_subsidi",
			beforeSend: function(){
				$('#loading').show();
				$('#error').hide();
			},
			success: function(msg){
				$('#loading').hide();
				$('#sisa').html(msg);
				$('#besar_subsidi').focus();
			},
			error:function(){
				$('#loading').hide();
				$('#error').show();
			}
		});		
		stopPropagation();		
	});
	// Combo pilih Kelas
	$("#id_m_kelas").change(function(){
		$("#subsidi").hide('slow');
		$("#subsidi2").hide('slow');
		$("#rincian").show('slow');
		var besar_subsidi = $("#besar_subsidi").val();
		$('#sub').text(besar_subsidi);
		var sisa_subsidi = $("#sisa_subsidi").val();
		$('#sisa_sub').text(sisa_subsidi);
		// Format Rp
		$('#besar_subsidi,#sub,#sisa_sub').priceFormat({
		    prefix: 'Rp ',
		    centsLimit: '',
		    thousandsSeparator: '.'
		});		

		var id_m_kelas = $("#id_m_kelas").val();
		var id_m_j_pendidikan = $("#id_m_j_pendidikan").val();
		var id_a_subsidi = $("#id_a_subsidi").val();
		var id_a_bulan = $("#id_a_bulan").val();
		var besar_subsidi = $("#besar_subsidi").unmask();
		$.ajax({
			type: "POST",				
			data: "id_m_j_pendidikan="+id_m_j_pendidikan+"&id_m_kelas="+id_m_kelas+"&id_a_subsidi="+id_a_subsidi+"&id_a_bulan="+id_a_bulan+"&besar_subsidi="+besar_subsidi,
			url : "<?php echo site_url(); ?>b_sub_sy/list_tholabah_modal",
			beforeSend: function(){
				$('#loadingTh').show();
				$('#errorTh').hide();
			},
			success: function(msg){
				$('#loadingTh').hide();
				$('#list_tholabah_modal').html(msg);
				$("#list_tholabah_modal").show('slow');	
			},
			error:function(){
				$('#loadingTh').hide();
				$('#errorTh').show();
			}
		});		
		stopPropagation();		
	});
	// Close alert/rincian subsidi
	$("#pilihLagi").click(function(){		
		$("#subsidi2").show('slow');
		$("#subsidi").show('slow');
		$("#id_m_kelas").hide('slow');
		$("#list_tholabah_modal").hide('slow');		
		$("#rincian").hide('slow');
		$('#besar_subsidi').focus();
	});
	// Enter pada textbox besar subsidi
	var code =null;
	$('#besar_subsidi').keypress(function(e){
	    code= (e.keyCode ? e.keyCode : e.which);
	    if (code == 13) {

			$("#subsidi").hide('slow');
			$("#subsidi2").hide('slow');
			$("#id_m_kelas").show('slow');
			$("#rincian").show('slow');
			$("#list_tholabah_modal").show('slow');	

			var besar_sub = $("#besar_subsidi").val();
			$('#sub').text(besar_sub); // Ambil value #besar_subsidi 

			// var sisa_subsidi = $("#sisa_subsidi").val();
			// $('#sisa_sub').text(sisa_subsidi); // Ambil value #sisa_subsidi

			// Menghitung sisa subsidi dikurangi tholabah yang dichecked
	        var jmlChecked = $('input:checkbox:checked').length; // Jumlah yang dicheck
	        var besar_subsidi = $("#besar_subsidi").unmask(); // Menghilangkan format Rp
	        var sisa_subsidi = $("#sisa_subsidi").val();
	        var total = jmlChecked * besar_subsidi;
	        var sisa = sisa_subsidi - total;
	        $('#sisa_sub').text(sisa);

			// Format Rp
			$('#besar_subsidi,#sub,#sisa_sub').priceFormat({
			    prefix: 'Rp ',
			    centsLimit: '',
			    allowNegative: true,
			    thousandsSeparator: '.'
			});
		}
	});
	// Checkbox dalam bentuk table row
    $('.diklik tr').click(function(event) {
        if (event.target.type !== 'checkbox') {
            $(':checkbox', this).trigger('click');
        }
    });

    $("input[type='checkbox']").change(function (e) {
        if ($(this).is(":checked")) {
            $(this).closest('tr').addClass("highlight_row");
        } else {
            $(this).closest('tr').removeClass("highlight_row");
        }
        //var numberNotChecked = $('input:checkbox:not(":checked")').length;
        // Menghitung sisa subsidi dikurangi tholabah yang dichecked
        var jmlChecked = $('input:checkbox:checked').length;
        var besar_sub = $("#besar_subsidi").unmask();
        var besar_subsidi = besar_sub;
        var sisa_subsidi = $("#sisa_subsidi").val();
        var total = jmlChecked * besar_subsidi;
        var sisa = sisa_subsidi - total;
  		$('#sisa_sub').text(sisa); 

  		// Format Rp
		$('#sisa_sub').priceFormat({
		    prefix: 'Rp ',
		    centsLimit: '',
		    allowNegative: true,
		    thousandsSeparator: '.'
		});    
		// Jika ada yang di checked dan sisa >= 0 maka tombol akan enabled/hilangkan disabled
  		var buttonsChecked = $('input:checkbox:checked');
	    if (buttonsChecked.length && sisa >= 0) {
	        $('#saveButton').removeAttr('disabled');
	    }else {
	    	$('#saveButton').attr('disabled', 'disabled');
	    }
    });
	// Save
	$('#SaveBagiSubSy').bind('submit', function(event) {
		var link = $(this).attr('action');
		var id_a_bulan = $("#id_a_bulan").val();
		var id_m_j_pendidikan = $("#id_m_j_pendidikan").val();
		var id_m_th_ajaran = $("#id_m_th_ajaran").val();
		$.ajax({
			url: link,
			type: "POST",
			data: $(this).serialize(),
			cache: false,
			beforeSend: function(){
				$('#errorSave').hide();
				$('#loadingSave').show();
			},          
			success: function() {
				var $container = $("#contentBagiSubSy");
				$container.load("<?php echo site_url(); ?>b_sub_sy/refresh/"+id_a_bulan+"/"+id_m_j_pendidikan+"/"+id_m_th_ajaran);

				var $container = $("#jml");
				$container.load("<?php echo site_url(); ?>b_sub_sy/refresh_jml/"+id_a_bulan+"/"+id_m_j_pendidikan+"/"+id_m_th_ajaran);
				
				$('#loadingSave').hide();
				$('#addModal').foundation('reveal', 'close');
			},
			error:function(){
				$('#loadingSave').hide();
				$('#errorSave').show();
			}
		});
		return false; // dont post it automatically
	});	
});
// Delete
$("#hapusBagiSubSy").on("click", function(){
	var id_b_sub_sy = $("#hpid").text();
	var id_a_bulan1 = $("#id_a_bulan1").val();
	var id_m_j_pendidikan1 = $("#id_m_j_pendidikan1").val();
	var id_m_th_ajaran1 = $("#id_m_th_ajaran1").val();
		$.ajax({
			type: "POST",
			data: "id_b_sub_sy="+id_b_sub_sy,
			url : "<?php echo site_url(); ?>b_sub_sy/delete",				
			cache: false,
			beforeSend: function(){
				$('#loadingDel').show();
				$('#errorDel').hide();
			}, 
			success: function(){
				var $container = $("#contentBagiSubSy");
				$container.load("<?php echo site_url(); ?>b_sub_sy/refresh/"+id_a_bulan1+"/"+id_m_j_pendidikan1+"/"+id_m_th_ajaran1);

				var $container = $("#jml");
				$container.load("<?php echo site_url(); ?>b_sub_sy/refresh_jml/"+id_a_bulan1+"/"+id_m_j_pendidikan1+"/"+id_m_th_ajaran1);
				
				$('#loadingDel').hide();
				$('#delModal').foundation('reveal', 'close');					
			}, 
			error:function(){
				$('#loadingDel').hide();
				$('#errorDel').show();
			}
		});
		return false; 
});
</script>